package com.gui.hospital.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gui.hospital.mapper.HospitalMapper;
import com.gui.hospital.model.Hospital;
import com.gui.hospital.service.HospitalService;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Author:刘舒佳
 * Date:2024/8/22
 */
@Service
@AllArgsConstructor
public class HospitalServiceImpl extends ServiceImpl<HospitalMapper, Hospital> implements HospitalService {
    private final HospitalMapper hospitalMapper;
    private final RedisTemplate redisTemplate;
    @Override
    public Page<Hospital> pageList(Hospital hospital,Integer currentPage,Integer pageSize) {
        Page<Hospital> hospitalPage = new Page<>();
        hospitalPage.setCurrent(currentPage);
        hospitalPage.setSize(pageSize);
        QueryWrapper<Hospital> hospitalQueryWrapper = new QueryWrapper<>();
        if(hospital != null){
            //中文名
            hospitalQueryWrapper.lambda().like(hospital.getChineseName() != null&&!"".equals(hospital.getChineseName()),Hospital::getChineseName,hospital.getChineseName());
            //英文名
            hospitalQueryWrapper.lambda().like(hospital.getEnglishName() != null&&!"".equals(hospital.getEnglishName()),Hospital::getEnglishName,hospital.getEnglishName());
            //地址
            hospitalQueryWrapper.lambda().like(hospital.getAddress() != null&&!"".equals(hospital.getAddress()),Hospital::getAddress,hospital.getAddress());
            //等级
            hospitalQueryWrapper.lambda().like(hospital.getHospitalLevel() != null&&!"".equals(hospital.getHospitalLevel()),Hospital::getHospitalLevel,hospital.getHospitalLevel());
            //是否医保
            hospitalQueryWrapper.lambda().eq(hospital.getIsInsurance() != null,Hospital::getIsInsurance,hospital.getIsInsurance());
        }
        Page<Hospital> hospitalPage1 = hospitalMapper.selectPage(hospitalPage, hospitalQueryWrapper);
        return hospitalPage1;
    }

    /**
     * 通过医院名称查询医院信息
     * @param name
     * @return
     */
    @Override
    public List<Hospital> findByName(String name) {

        if(name==null){
            //先从redis中获取数据，如果没有就从数据库获取，再将获取的数据添加进redis中
            Object list = redisTemplate.boundValueOps("registration_hospitalList").get();
            if(list!=null){
                return (List<Hospital>)list;
            }
            List<Hospital> hospitals = hospitalMapper.selectList(null);
            redisTemplate.boundValueOps("registration_hospitalList").set(hospitals);
            return hospitals;

        }
        QueryWrapper<Hospital> hospitalQueryWrapper = new QueryWrapper<>();
        hospitalQueryWrapper.lambda().like(name!=null&&!"".equals(name),Hospital::getChineseName,name);
        List<Hospital> hospitals = hospitalMapper.selectList(hospitalQueryWrapper);
        return hospitals;
    }
}

